/**
 * Copyright (C) 2010 Alfredo Morresi
 * 
 * This file is part of RainbowLibs project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.rainbowbreeze.libs.helper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;
import android.text.TextUtils;

/**
 * Helpers for string parsing etc
 * 
 * @author Alfredo "Rainbowbreeze" Morresi
 *
 */
public class RainbowStringHelper {
	//---------- Private fields

    
    //---------- Constructors


	//---------- Public properties

    
	//---------- Public methods
	public static String getStringBetween(String source, String tokenBefore, String tokenAfter)
	{
		String result = "";
		
		if (TextUtils.isEmpty(source) || TextUtils.isEmpty(tokenBefore))
			return result;
		
		int posInit = source.indexOf(tokenBefore);
		if (-1 == posInit) return result;
		posInit = posInit += tokenBefore.length();
		
		int posEnd;
		if (TextUtils.isEmpty(tokenAfter)) {
			posEnd = source.length();
		} else {
			posEnd = source.indexOf(tokenAfter, posInit);
			if (-1 == posEnd) posEnd = source.length(); 
		}
		
		result = source.substring(posInit, posEnd);
		
		return result;
	}

	
	/**
	 * Return the latest char of the string before the variable part
	 * @param stringToCheck
	 * @return
	 */
	
	public static String getInvariableStringFinalBoundary(String stringToCheck)
	{
		if (TextUtils.isEmpty(stringToCheck)) return stringToCheck;
		//ok, i know it isn't the best way, but it works as a workaround
		//for the presence of %s parameter in the source message string ;)
		int pos = stringToCheck.indexOf("%");
		return (pos >= 0) ? stringToCheck.substring(0, pos) : stringToCheck;
	}
	
	
	/**
	 * Hides the last digits of a telephone number
	 * @param numberToScramble
	 * @return
	 */
	public static String scrambleNumber(String numberToScramble)
	{
		if (TextUtils.isEmpty(numberToScramble) || numberToScramble.length() < 3) {
			return numberToScramble;
		} else {
			return numberToScramble.substring(0, numberToScramble.length() - 3) + "***";
		}
	}
	
	
    /**
     * Clean phone number from [ -.()<>].
     * 
     * @param phoneNumber
     *            recipient's mobile number
     * @return clean number
     */
    public static String cleanPhoneNumber(final String phoneNumber) {
        if (phoneNumber == null) return "";

        return phoneNumber.replaceAll("[^+0-9]", "").trim();
    }	


	/**
	 * Join an array of strings in a single string
	 * @param array
	 * @param separator
	 * @return
	 */
	public static String join(String [] array, CharSequence separator)
	{
		if (null == array) return null;
		
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < array.length; i++) {
			sb.append(array[i]);
			if (i != array.length -1) sb.append(separator);
		}
		return sb.toString();
	}
	
	
	
	public static String readContentFromResourceFile(Context context, int resourceId)
	throws IOException {
	    StringBuffer sb = new StringBuffer();
	    final String NEW_LINE = System.getProperty("line.separator");
	    
	    InputStream is = context.getResources().openRawResource(resourceId);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String readLine = null;

        try {
            while ((readLine = br.readLine()) != null) {
                sb.append(readLine);
                sb.append(NEW_LINE);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            br.close();
            is.close();
        }
        
        return sb.toString();
	}
	

	//---------- Private methods
}
